import { http, setToken, removeToken } from '@/utils'

export const login = (loginData) => {
  return async (dispatch) => {
    const res = await http.post('/authorizations', loginData)

    const {
      data: { token },
      message,
    } = res.data

    if (message === 'OK') {
      setToken(token)

      dispatch({ type: 'login/setToken', payload: token })
    }
  }
}

export const logout = () => {
  return (dispatch) => {
    dispatch({ type: 'logout/clearToken' })
    removeToken()
    dispatch({ type: 'user/clearInfo' })
  }
}
